<?php 
/**
 * console的model
 * Enter description here ...
 * @author Larry
 *
 */
class ConsoleModel extends Model
{
	//数据表
	protected $trueTableName = 'cmp_console'; 

	//设置主键
	protected  $key = 'user_id';
	
		/**
		 * 
		 * 用户登陆
		 */
		public function checkLogin($user, $pass)
		{
			
			//密码加密
			$pass = md5($pass);
			
			//校验查询
			$restul = $this->field('user_id,username')->where("username='{$user}' and password='{$pass}'")->find();
			
			if ( $restul['user_id'] == NULL ) return false;
			
			return $restul;
			
		}
		/**
		 * 通过ID 获取一条数据
		 * @param unknown $id 主键ID
		 * @return Ambigous <mixed, boolean, NULL, multitype:, unknown, string>
		 */
		public function getRowById($id)
		{
			
			//字段
			$field = array(
						
					'user_id',
					'username',
						
			);
				
			//条件
			$where = $this->key."=".$id;
			
			return $this->field($field)->where($where)->find();
			
		}
		/**
		 * 校验分类名称是否存在
		 * @param unknown $field
		 * @return boolean|Ambigous <>
		 */
		public function exist_name($name)
		{
				
			//字段
			$field = array(
						
					'username'
		
			);
			
			//条件
			$where = "username='{$name}'";
				
			$result = $this->field('username')->where($where)->find();
			//不存在
			if ( $result['username'] == NULL ) return false;
				
			//存在
			return $result['username'];
				
		}
		/**
		 * 获取name根绝ID
		 * @param unknown $id
		 * @return Ambigous <mixed, boolean, NULL, multitype:, unknown, string>
		 */
		public function getNameById($id)
		{
				
			//字段
			$field = array(
		
					'username'
		
			);
				
			//条件
			$where = $this->key.'='.$id;
				
			return $this->field($field)->where($where)->find();
				
		}
		/**
		 * 更新一条数据
		 * @param unknown $arr
		 * @return string
		 */
		public function update($arr)
		{
			
			
			//业务逻辑处理 Start
			$checkName = $this->exist_name($arr['username']);
			
			//获取原有名称
			$name = $this->getNameById($arr['user_id']);
			
			//die($name['username'].'=>'.$checkName);
		
			if ( $arr['username'] == NULL )
			{
				$msg_data['status'] = 0;
					
				$msg_data['info'] = '名称不能为空';
					
				return $msg_data;
			}
				
			//条件
			$where = $this->key.'='.$arr['user_id'];
			
			if ( $this->where($where)->save($arr) !== false )
			{
			
				$msg_data['status'] = 1;
					
				$msg_data['info'] = '修改成功';
			
			}
			else
			{
				$msg_data['status'] = 0;
					
				$msg_data['info'] = '修改失败';
			}
			
			return $msg_data;
			
			//End
			
		}
}


?>